﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Media;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework;

namespace theguineasyndrome.levels
{
    public class PyroLevel : LevelLoader
    {

        private bool wagon1Activated;
        private bool wagon2Activated;

        public PyroLevel(GameMode gameMode, ContentManager content, WaterWeapon waterWeapon, FireWeapon fireWeapon, PortalWeapon portalWeapon, PaintWeapon paintWeapon)
            : base(gameMode, content, waterWeapon, fireWeapon, portalWeapon, paintWeapon)
        {
            wagon1Activated = false;
            wagon2Activated = false;
        }

        private void switch1On(Switch switch1)
        {
            wagon1Activated = true;
        }

        private void switch2On(Switch switch2)
        {
            wagon2Activated = true;
        }

        private void wagon1Updater(ObstacleElement wagon, GameTime gameTime)
        {
            if (wagon1Activated)
            {
                wagon.Position += new Vector2((float)gameTime.ElapsedGameTime.TotalSeconds * 90, 0);

                if (wagon.Position.X > level.getPointMarkPosition("endwagon 1").X)
                    wagon1Activated = false;

                if (wagon.Character != null)
                    wagon.Character.setPosition(wagon.Position + wagon.CharacterRelativePosition);
            }
        }

        private void wagon2Updater(ObstacleElement wagon, GameTime gameTime)
        {
            if (wagon2Activated)
            {
                wagon.Position += new Vector2((float)gameTime.ElapsedGameTime.TotalSeconds * 160, 0);

                if (wagon.Position.X > level.getPointMarkPosition("endwagon 2").X)
                    wagon2Activated = false;

                if (wagon.Character != null)
                    wagon.Character.setPosition(wagon.Position + wagon.CharacterRelativePosition);
            }
        }

        private void elevator1Updater(ObstacleElement elevator, GameTime gameTime)
        {
            elevator.Center = level.getPointMarkPosition("UpAndDown Elevator 1") + new Vector2(0, (float)(Math.Sin(gameTime.TotalGameTime.TotalSeconds) * 140));
            if (elevator.Character != null)
                elevator.Character.setPosition(elevator.Position + elevator.CharacterRelativePosition);
        }

        private void elevator2Updater(ObstacleElement elevator, GameTime gameTime)
        {
            elevator.Center = level.getPointMarkPosition("UpAndDown Elevator 2") + new Vector2(0, (float)(Math.Cos(gameTime.TotalGameTime.TotalSeconds) * 200));
            if (elevator.Character != null)
                elevator.Character.setPosition(elevator.Position + elevator.CharacterRelativePosition);
        }

        public override Level load()
        {
            level = getLevel("levels/level3");
            //level = getCustomLevel("level");

            Texture2D goombaTexture = content.Load<Texture2D>("images/enemies/goomba");
            for (int i = 1; i <= 4; i++)
                level.addElement(new Goomba(level, goombaTexture, level.getPointMarkPosition("enemyMushroom " + i)));

            Texture2D turtleTexture = content.Load<Texture2D>("images/enemies/turtle");
            for (int i = 1; i <= 3; i++)
                level.addElement(new Turtle(level, turtleTexture, level.getPointMarkPosition("enemyTortue " + i)));

            Texture2D beeTexture = content.Load<Texture2D>("images/enemies/bee");
            Texture2D beeRopeTexture = content.Load<Texture2D>("images/enemies/beerope");
            for (int i = 1; i <= 7; i++)
                level.addElement(new Bee(level, beeTexture, beeRopeTexture, level.getPointMarkPosition("enemyBee " + i)));

            Texture2D knightTexture = content.Load<Texture2D>("images/enemies/knight");
            level.addElement(new Knight(level, knightTexture, level.getPointMarkPosition("enemyKnight 1")));

            // wagon 1
            Texture2D wagonTexture = content.Load<Texture2D>("images/level/level/obstacles/wagon1");

            ObstacleElement wagon1 = new ObstacleElement(level, wagonTexture, level.getPointMarkPosition("wagon 1"));
            wagon1.setUpdater(wagon1Updater);
            level.addElement(wagon1);

            // wagon 2
            ObstacleElement wagon2 = new ObstacleElement(level, wagonTexture, level.getPointMarkPosition("wagon 2"));
            wagon2.setUpdater(wagon2Updater);
            level.addElement(wagon2);

            // switch 1
            Texture2D switchTexture = content.Load<Texture2D>("images/level/level/obstacles/switch");
            Texture2D switchBlockTexture = content.Load<Texture2D>("images/level/level/obstacles/switchblock");

            Switch switch1 = new Switch(level, switchTexture, switchBlockTexture, level.getPointMarkPosition("switch 1"));
            switch1.setOn(switch1On);
            level.addElement(switch1);
            switch1.addBlockToLevel();

            // switch 2
            Switch switch2 = new Switch(level, switchTexture, switchBlockTexture, level.getPointMarkPosition("switch 2"));
            switch2.setOn(switch2On);
            level.addElement(switch2);
            switch2.addBlockToLevel();

            // elevator 1
            Texture2D elevatorTexture = content.Load<Texture2D>("images/level/level/obstacles/elevator");

            ObstacleElement elevator1 = new ObstacleElement(level, elevatorTexture, level.getPointMarkPosition("UpAndDown Elevator 1"));
            elevator1.setUpdater(elevator1Updater);
            level.addElement(elevator1);

            // elevator 2
            ObstacleElement elevator2 = new ObstacleElement(level, elevatorTexture, level.getPointMarkPosition("UpAndDown Elevator 2"));
            elevator2.setUpdater(elevator2Updater);
            level.addElement(elevator2);

            addCarots(level);

            Song song = content.Load<Song>("music/Absurdity");
            MediaPlayer.Stop();
            MediaPlayer.IsRepeating = true;
            MediaPlayer.Play(song);

            return level;
        }

    }
}
